package com.maybach.core.ao.impl;

import com.maybach.common.BizResult;
import com.maybach.common.enums.order.LogisticsStatusEnum;
import com.maybach.common.model.LogisticsCompanyDO;
import com.maybach.common.query.LogisticsCompanyQuery;
import com.maybach.component.city.CityData;
import com.maybach.component.city.domain.City;
import com.maybach.core.ao.LogisiticsCompanyAO;
import com.maybach.core.form.logistics.LogisticsCompanyForm;
import com.maybach.core.form.logistics.LogisticsCompanyQueryForm;
import com.maybach.core.manager.LogisticsCompanyManager;
import com.maybach.util.AssertUtil;
import com.maybach.util.ImgUrlUtil;
import com.maybach.util.query.support.PageResult;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by weishuai on 16/2/23.
 */
@Component
public class LogisiticsCompanyAOImpl implements LogisiticsCompanyAO {
    @Autowired
    LogisticsCompanyManager logisticsCompanyManager;
    Logger logger = LoggerFactory.getLogger(this.getClass());


    @Override
    public PageResult<LogisticsCompanyDO> queryLogisticsCompany(LogisticsCompanyQueryForm queryForm) {
        try {
            LogisticsCompanyQuery query = this.assemblyQuery(queryForm);
            return logisticsCompanyManager.selectByQueryWithPage(query);
        } catch (Exception e) {
            logger.error("error in queryLogisticsCompany:form:"+queryForm.toString());
            return new PageResult<LogisticsCompanyDO>();
        }
    }

    @Override
    public boolean updateLogisticsCompany(LogisticsCompanyForm queryForm) {
        LogisticsCompanyDO logisticsCompanyDO = this.assemblyDo(queryForm);
        return logisticsCompanyManager.updateByPrimaryKeySelective(logisticsCompanyDO) > 0;
    }

    @Override
    public Long addLogisticsCompany(LogisticsCompanyForm logisticsCompanyForm) {
        LogisticsCompanyDO logisticsCompanyDO = this.assemblyDo(logisticsCompanyForm);
        logisticsCompanyManager.insertSelective(logisticsCompanyDO);
        return logisticsCompanyDO.getId();
    }

    @Override
    public BizResult updateStatus(Long id, Integer status) {
        BizResult result = new BizResult();
        AssertUtil.notNull(id, "id");
        AssertUtil.notNull(status, "status");
        LogisticsStatusEnum logisticsStatusEnum = LogisticsStatusEnum.getStatus(status);
        AssertUtil.notNull(logisticsStatusEnum, "logisticsStatusEnum");
        LogisticsCompanyDO logisticsCompanyDO = logisticsCompanyManager.selectByPrimaryKey(id);
        if (null == logisticsCompanyDO) {
            result.setResultMsg("数据不存在");
        } else if (logisticsCompanyDO.getStatus().equals(status)) {
            result.setResultMsg("数据已更新");
        } else {

            logisticsCompanyDO.setStatus(status);
            logisticsCompanyManager.updateByPrimaryKeySelective(logisticsCompanyDO);
        }
        return result;
    }


    @Override
    public LogisticsCompanyForm getLogisticsCompanyFormById(Long id) {
        LogisticsCompanyDO logisticsCompanyDO = logisticsCompanyManager.selectByPrimaryKey(id);
        if (null == logisticsCompanyDO) {
            return null;
        }
        return this.assemblyForm(logisticsCompanyDO);
    }

    @Override
    public List<LogisticsCompanyDO> getAllLogisticsCompany(LogisticsStatusEnum status) {
        LogisticsCompanyQuery query=new LogisticsCompanyQuery();
        query.createCriteria().andStatusEqualTo(status.getCode());
        return logisticsCompanyManager.selectByQuery(query);
    }

    protected LogisticsCompanyQuery assemblyQuery(LogisticsCompanyQueryForm queryForm) {
        AssertUtil.notNull(queryForm, "queryForm");
        LogisticsCompanyQuery query = new LogisticsCompanyQuery();
        LogisticsCompanyQuery.Criteria criteria = query.or();
        query.setPageNo(queryForm.getPageNo());
        query.setPageSize(queryForm.getPageSize());

        if (StringUtils.isNotBlank(queryForm.getCompanyName())) {
            criteria.andCompanyNameLike("%" + queryForm.getCompanyName() + "%");
        }
        if (StringUtils.isNotBlank(queryForm.getContactsPhone())) {
            criteria.andContractPhoneLike("%" + queryForm.getContactsPhone() + "%");
        }
        if (queryForm.getStatus() != null) {
            criteria.andStatusEqualTo(queryForm.getStatus());
        }
        return query;
    }

    protected LogisticsCompanyDO assemblyDo(LogisticsCompanyForm queryForm) {
        AssertUtil.notNull(queryForm, "queryForm");
        LogisticsCompanyDO logisticsCompanyDO = new LogisticsCompanyDO();
        City city =CityData.getCityById(queryForm.getCityId());
        if(city!=null){
            logisticsCompanyDO.setCityName(city.getName());
        }
        logisticsCompanyDO.setCityId(queryForm.getCityId());
        logisticsCompanyDO.setCompanyName(queryForm.getCompanyName());
        logisticsCompanyDO.setContractName(queryForm.getContactsName());
        logisticsCompanyDO.setDetailAddress(queryForm.getDetailAddress());
        logisticsCompanyDO.setProvinceId(queryForm.getProvinceId());
        logisticsCompanyDO.setContractPhone(queryForm.getContactsPhone());
        logisticsCompanyDO.setMemo(queryForm.getMemo());
        logisticsCompanyDO.setStatus(queryForm.getStatus());
        logisticsCompanyDO.setInsuranceImgs(ImgUrlUtil.mergeList(queryForm.getInsuranceImgs()));
        logisticsCompanyDO.setInsuranceType(queryForm.getInsuranceType());
        logisticsCompanyDO.setId(queryForm.getId());
        return logisticsCompanyDO;
    }

    protected LogisticsCompanyForm assemblyForm(LogisticsCompanyDO logisticsCompanyDO) {
        AssertUtil.notNull(logisticsCompanyDO, "logisticsCompanyDO");
        LogisticsCompanyForm form=new LogisticsCompanyForm();
        form.setInsuranceType(logisticsCompanyDO.getInsuranceType());
        form.setStatus(logisticsCompanyDO.getStatus());
        form.setMemo(logisticsCompanyDO.getMemo());
        form.setCompanyName(logisticsCompanyDO.getCompanyName());
        form.setContactsPhone(logisticsCompanyDO.getContractPhone());
        form.setContactsName(logisticsCompanyDO.getContractName());
        form.setProvinceId(logisticsCompanyDO.getProvinceId());
        form.setDetailAddress(logisticsCompanyDO.getDetailAddress());
        form.setId(logisticsCompanyDO.getId());
        form.setCityId(logisticsCompanyDO.getCityId());
        form.setProvinceName(CityData.provinceCn(logisticsCompanyDO.getProvinceId()));
        form.setInsuranceImgs(ImgUrlUtil.parseList(logisticsCompanyDO.getInsuranceImgs()));
        return form;
    }
}
